package com.neulogistics.core.substation.api.application.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationPaymentViewModel;
import com.neulogistics.core.substation.domain.model.valueobject.CourierInfo;
import com.neulogistics.core.substation.domain.model.valueobject.CustomInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderItem;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPaymentPO;
import org.springframework.beans.BeanUtils;

public class SubstationPaymentPOVMConverter {
    private static final int Rate = 100;

    public static SubstationPaymentViewModel POToVM(SubstationPaymentPO substationPaymentPO){
        if (substationPaymentPO == null)
            return null;

        SubstationPaymentViewModel substationPaymentViewModel = new SubstationPaymentViewModel();
        BeanUtils.copyProperties(substationPaymentPO, substationPaymentViewModel);
        // long转string
        substationPaymentViewModel.setPaymentId(substationPaymentPO.getPaymentId().toString());
        substationPaymentViewModel.setTaskId(substationPaymentPO.getTaskId().toString());

        // json对象转换
        substationPaymentViewModel.setCustomInfo(JSONUtil.toBean(substationPaymentPO.getCustomInfo(), CustomInfo.class));
        substationPaymentViewModel.setCourierInfo(JSONUtil.toBean(substationPaymentPO.getCourierInfo(), CourierInfo.class));
        substationPaymentViewModel.setOrderItems(JSONUtil.toList(substationPaymentPO.getOrderItems(), OrderItem.class));
        // 金额转换
        substationPaymentViewModel.setPaymentTotal(Double.valueOf(substationPaymentPO.getPaymentTotal()) / Rate);

        return substationPaymentViewModel;
    }
}
